﻿CREATE PROCEDURE dbo.sec__GetColumnInPrimaryKey
	@TableName sysname = t_Address,
	@ColumnNumber int,
	@Key sysname = null output
AS

DECLARE @Count varchar,@Keys nvarchar(4000)
CREATE TABLE #PK(
index_name sysname,
index_description varchar(210),
index_keys nvarchar(2078)
)

INSERT INTO #PK 
exec sp_helpindex @TableName

SELECT @Keys = index_keys FROM #PK WHERE index_name LIKE 'PK_%'

SELECT @Count = COUNT(*) FROM Split(@Keys,', ')

IF @Count < @ColumnNumber RETURN

IF @ColumnNumber = 2
	SELECT TOP 2 @Key = Element FROM Split(@Keys,', ')

IF @ColumnNumber = 3
	SELECT TOP 3 @Key = Element FROM Split(@Keys,', ')




